package com.lehuowujie.lehuomarket.fragment;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.SpaceDecoration;
import com.lehuowujie.lehuomarket.R;
import com.lehuowujie.lehuomarket.adapter.HomeAdapter;
import com.lehuowujie.lehuomarket.adapter.HomeBannerLoader;
import com.lehuowujie.lehuomarket.adapter.HomeCategoryAdapter;
import com.lehuowujie.lehuomarket.base.BaseFragment;
import com.lehuowujie.lehuomarket.ui.CategoryActivity;
import com.lehuowujie.lehuomarket.ui.FlowActivity;
import com.lehuowujie.lehuomarket.ui.ShopDetailActivity;
import com.lehuowujie.lehuomarket.util.Logger;
import com.lehuowujie.lehuomarket.util.ScreenUtil;
import com.lehuowujie.lehuomarket.util.ToastUtil;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页
 * Created by 王瑞铭 on 2017/1/13.
 */
public class HomeFragment extends BaseFragment implements View.OnClickListener {

    public static final String TAG = HomeFragment.class.getName();

    public static HomeFragment mFragment;

    public static HomeFragment getInstance() {
        if (mFragment == null) {
            synchronized (HomeFragment.class) {
                if (mFragment == null)
                    mFragment = new HomeFragment();
            }
        }
        return mFragment;
    }

    private EasyRecyclerView mRecyclerView;
    private List<String> mHomeList;
    private HomeAdapter mHomeAdapter;

    private List<Integer> mBannerList = new ArrayList<>();

    private RelativeLayout homeToolBarLayout;

    private ImageView topImg;

    private int screenHeight;

    private int scrollY;

    @Override
    public int createView() {
        return R.layout.fragment_home;
    }

    @Override
    public void initView() {
        screenHeight = ScreenUtil.getDiaplayMetricsHeight(getActivity());
        mRecyclerView = (EasyRecyclerView) mContentView.findViewById(R.id.recyclerView);
        homeToolBarLayout = (RelativeLayout) mContentView.findViewById(R.id.home_toolbar);
        topImg = (ImageView) mContentView.findViewById(R.id.top_img);
        initRecyclerView();
    }

    @Override
    public void initEvent() {
        topImg.setOnClickListener(this);
        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                scrollY += dy;
                float alpha = scrollY * 1.0f / 200;
                homeToolBarLayout.setAlpha(alpha);
                if (scrollY >= screenHeight) {
                    topImg.setVisibility(View.VISIBLE);
                } else {
                    topImg.setVisibility(View.GONE);
                }
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        mHomeAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                startActivity(new Intent(getActivity(), FlowActivity.class));
            }
        });
    }

    private void initRecyclerView() {
        final GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
        mRecyclerView.setLayoutManager(manager);
        SpaceDecoration itemDecoration = new SpaceDecoration((int) ScreenUtil.convertDpToPixel(10, getActivity()));//params is height
        itemDecoration.setPaddingEdgeSide(true);//whether add space for left and right adge.default is true.
        itemDecoration.setPaddingStart(true);//whether add top space for the first line item(exclude header).default is true.
        itemDecoration.setPaddingHeaderFooter(false);//whether add space for header and footer.default is false.
        mRecyclerView.addItemDecoration(itemDecoration);
        getHomeData();
        mHomeAdapter = new HomeAdapter(getActivity());
        addHeader();
        mHomeAdapter.addAll(mHomeList);
        mRecyclerView.setAdapter(mHomeAdapter);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 0) {
                    return manager.getSpanCount();
                } else {
                    return 1;
                }
            }
        });
    }

    private void addHeader() {
        mHomeAdapter.addHeader(new RecyclerArrayAdapter.ItemView() {
            @Override
            public View onCreateView(ViewGroup parent) {
                return LayoutInflater.from(getActivity()).inflate(R.layout.home_header_layout, parent, false);
            }

            @Override
            public void onBindView(View headerView) {
                //添加数据
                Banner banner = (Banner) headerView.findViewById(R.id.banner);
                EasyRecyclerView categoryGridView = (EasyRecyclerView) headerView.findViewById(R.id.categoryGridView);
                setBannerList(banner);
                initCategoryGridView(categoryGridView);
            }
        });
    }

    private void getHomeData() {
        mHomeList = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            mHomeList.add("");
        }
    }

    /**
     * 设置头布局中的轮播图数据
     *
     * @param banner
     */
    private void setBannerList(Banner banner) {
        mBannerList.clear();
        mBannerList.add(R.mipmap.banner1);
        mBannerList.add(R.mipmap.banner2);
        mBannerList.add(R.mipmap.banner3);
        banner.setImageLoader(new HomeBannerLoader());
        banner.setImages(mBannerList);
        banner.isAutoPlay(true);
        banner.setDelayTime(2000);
        banner.setIndicatorGravity(BannerConfig.CENTER);
        banner.start();
        banner.setOnBannerClickListener(new OnBannerClickListener() {
            @Override
            public void OnBannerClick(int position) {
                startActivity(new Intent(getActivity(), ShopDetailActivity.class));
            }
        });
    }

    /**
     * 初始化头布局中的分类
     *
     * @param categoryGridView
     */
    private void initCategoryGridView(EasyRecyclerView categoryGridView) {
        categoryGridView.setLayoutManager(new GridLayoutManager(getActivity(), 5));
        SpaceDecoration itemDecoration = new SpaceDecoration((int) ScreenUtil.convertDpToPixel(0, getActivity()));//params is height
        itemDecoration.setPaddingEdgeSide(true);//whether add space for left and right adge.default is true.
        itemDecoration.setPaddingStart(true);//whether add top space for the first line item(exclude header).default is true.
        itemDecoration.setPaddingHeaderFooter(false);//whether add space for header and footer.default is false.
        categoryGridView.addItemDecoration(itemDecoration);
        List<String> list = new ArrayList<>();
        list.add("京东超市");
        list.add("全球购");
        list.add("服装城");
        list.add("京东生鲜");
        list.add("京东到家");
        list.add("充值中心");
        list.add("领京豆");
        list.add("领券");
        list.add("惠赚钱");
        list.add("全部");
        HomeCategoryAdapter mHomeCategoryAdapter = new HomeCategoryAdapter(getActivity());
        categoryGridView.setAdapter(mHomeCategoryAdapter);
        mHomeCategoryAdapter.addAll(list);
        mHomeCategoryAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                startActivity(new Intent(getActivity(), CategoryActivity.class));
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.top_img:
                mRecyclerView.scrollToPosition(0);
                homeToolBarLayout.setAlpha(0.0f);
                topImg.setVisibility(View.GONE);
                break;
        }
    }
}
